import { Command } from '../constant';
import Taro, { getCurrentInstance } from '@tarojs/taro';
import { Dispatch } from 'typings';
import { IAllReducerProps } from '../types';
import { getReducerData } from '@/redux/store';
import { extraPathsValue } from '@/redux/util';
import api from 'api';

export default (dispatch: Dispatch) => {
  let action = {
    commonChange(...param: any) {
      dispatch({
        type: Command.commonChange,
        payload: extraPathsValue(...arguments),
      });
    },
    async page() {
      let itemStr = Taro.getStorageSync('b2b-wechat@login');
      if (itemStr) {
        this.complaintList();
      }
    },
    async complaintList() {
      let { user } = Taro.getStorageSync('b2b-wechat@login');
      action.commonChange('main.isLoading', true);
      let { pageNum, complaintsList, curNow } = getData().main;
      const params = {
        userId: user?.id || '',
        pageNum,
        status: curNow
      };
      let res = await api.repairController.complaintList(params);
      if (res.code == '200') {
        this.commonChange('main.totalPage', res.data.totalPage);
        if (pageNum == 0) {
          action.commonChange('main.complaintsList', res.data.rows)
        } else {
          let newList = complaintsList.concat(
            res.data.rows,
          );
          action.commonChange('main.complaintsList', newList)
        }
      }
      this.commonChange('main.isLoading', false);
    }
  };
  return action;
};

function getData(): IAllReducerProps {
  return {
    main: getReducerData('packageAComplaintsMain'),
  };
}

//create by moon https://github.com/creasy2010/moon
